import { AxiosInstance} from 'axios';
import axios from 'axios'

const instance: AxiosInstance = axios.create({
  baseURL: 'http://localhost:8081/goods',
  timeout: 2000,
  headers: {
    'Content-type': 'application/json'
  },

  withCredentials: true
})

class GoodsService {
  static async findAll(map?: {like?: string, type?: string}) {
    if(map) {
      let response = await instance.get('/', {
	params: {
	  like: map.like,
	  type: map.type
	}
      })

      return response.data
    } else {
      let response = await instance.get('/')
      return response.data
    }
  }


  static async insertOne(goods: Goods) {
    let response = await instance.post('/', JSON.stringify(goods))
    return response.data
  }

  static async deleteOne(id: number) {
    let response = await instance.delete('/', {
      params: {
	id
      }
    })
    return response.data
  }

  static async updateOne(goods: Goods) {
    let response = await instance.put('/', JSON.stringify(goods))
    return response.data
  }
}

export default GoodsService